/**
 *
 */
package com.teradata.dashboard.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import com.teradata.dashboard.DashboardApplication;
import com.teradata.dashboard.constant.DashboardConstant;
/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂ava联盟</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class HttpUtil
{
	public static enum HttpMethod
    {
        GET, POST
    }
	// 创建HttpClient对象
	public static HttpClient httpClient = new DefaultHttpClient();
	
	 private static AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
	public static final String BASE_URL =DashboardConstant.getPara("BASEURL", "");
	/**
	 *
	 * @param url 发送请求的URL
	 * @return 服务器响应字符串
	 * @throws Exception
	 */
	public static String getRequest(final String url)
		throws Exception
	{
		FutureTask<String> task = new FutureTask<String>(
		new Callable<String>()
		{
			@Override
			public String call() throws Exception
			{
				// 创建HttpGet对象。
				HttpGet get = new HttpGet(url);
				// 发送GET请求
				HttpResponse httpResponse = httpClient.execute(get);
				// 如果服务器成功地返回响应
				if (httpResponse.getStatusLine()
					.getStatusCode() == 200)
				{
					// 获取服务器响应字符串
					String result = EntityUtils
						.toString(httpResponse.getEntity());
					return result;
				}
				return null;
			}
		});
		new Thread(task).start();
		return task.get();
	}

	/**
	 * @param url 发送请求的URL
	 * @param params 请求参数
	 * @return 服务器响应字符串
	 * @throws Exception
	 */
	public static String postRequest(final String url
		, final Map<String ,String> rawParams)throws Exception
	{
		FutureTask<String> task = new FutureTask<String>(
		new Callable<String>()
		{
			@Override
			public String call() throws Exception
			{
				
				
				// 创建HttpPost对象。
				HttpPost post;
				
				post = new HttpPost(url);
				post.setHeader("Cookie", DashboardConstant.AUTH_KEY_COOKIE_NAME+ "=" + DashboardApplication.app.getMyCookie());
				//post.setHeader(header);
				// 如果传递参数个数比较多的话可以对传递的参数进行封装
				List<NameValuePair> params = 
					new ArrayList<NameValuePair>();
				for(String key : rawParams.keySet())
				{
					//封装请求参数
					params.add(new BasicNameValuePair(key 
						, rawParams.get(key)));
				}
				// 设置请求参数
				post.setEntity(new UrlEncodedFormEntity(
					params, "gbk"));
				// 发送POST请求
				HttpResponse httpResponse = httpClient.execute(post);
				// 如果服务器成功地返回响应
				if (httpResponse.getStatusLine()
					.getStatusCode() == 200)
				{
					// 获取服务器响应字符串
					String result = EntityUtils
						.toString(httpResponse.getEntity());
					System.out.println(params);
					System.out.println(result);
					return result;
				}
				return null;
			}
		});
		new Thread(task).start();
		return task.get();
	}
	
	public static void get(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
		asyncHttpClient.get(getAbsoluteUrl(url), params, responseHandler);
	  }

	  public static void post(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
		  asyncHttpClient.post(getAbsoluteUrl(url), params, responseHandler);
	  }

	  private static String getAbsoluteUrl(String relativeUrl) {
	      return BASE_URL + relativeUrl;
	  }
	
	public static void initCookieManager(){
		PersistentCookieStore myCookieStore = new PersistentCookieStore(DashboardApplication.app);
		asyncHttpClient.setCookieStore(myCookieStore);
		asyncHttpClient.addHeader("Cookie", DashboardConstant.AUTH_KEY_COOKIE_NAME+ "=" + DashboardApplication.app.getMyCookie());
		
		BasicClientCookie newCookie = new BasicClientCookie(DashboardConstant.AUTH_KEY_COOKIE_NAME, DashboardApplication.app.getMyCookie());
		newCookie.setVersion(1);
		newCookie.setDomain("101.227.243.40");
		newCookie.setPath("tpif");
		Calendar localCalendar = Calendar.getInstance();
		localCalendar.set(2025, 11, 29);		
		newCookie.setExpiryDate(localCalendar.getTime());
		
		myCookieStore.addCookie(newCookie);
		
		
	}
 
}
